const Router = require('koa-router')
const { CommentController } = require('../../controllers')
const { HttpDataResolve, Validator } = require('../../common')
const { Auth, SP_ADMIN, ADMIN } = require('../../middleware/auth')
const res = new HttpDataResolve()

const router = new Router({
  prefix: '/api/v1/comment'
})

// 创建评论
router.post('/', new Auth().m, async ctx => {
  const data = new Validator(ctx, {
    article_id: 'required|number',
    content: 'required',
    column_id: 'number',
    column_user_id: ''
  })

  await CommentController.create(data, ctx.auth);
  
  ctx.response.status = 200;
  ctx.body = res.success('发表成功');
})

// 删除评论
router.delete('/:id', new Auth().m, async ctx => {
  const data = new Validator(ctx.params, {
    id: 'required|number'
  }, true);

  await CommentController.delete(data, ctx);

  ctx.response.status = 200;
  ctx.body = res.success('删除成功')
})

// 获取评论
router.get('/list', new Auth().l, async ctx => {
  const data = new Validator(ctx.query, {
    page: 'number',
    size: 'number',
    article_id: 'number',
    user_id: 'number',
    keyword: '',
    order: 'include:DESC,ASC',
    column: 'include:true',
    column_id: 'number',
  }, true);

  const result = await CommentController.getList(data, ctx.auth);

  ctx.response.status = 200;
  ctx.body = res.json(result)
})

module.exports = router
